1433F - Zero Remainder Sum - CodeForces Solution


dp *2100

Please click on ads to support us..

C++ Code:

#include <bits/stdc++.h>
using namespace std;
#define all(c) (c).begin(), (c).end()
#define rall(A) A.rbegin(),A.rend()
#define pb push_back
#define f first
#define s second 
#define dbg(x) do {cerr << #x <<" = " << (x) << endl; } while (false)
//#define dbg(x) do {} while (false)
#define raya do {cerr << "===========================" << endl; } while (false)
//#define raya do {} while (false)
#define bpc __builtin_popcountll//numero de bits para long long
#define bclz __builtin_clzll//leading zeros para ll 
#define max_bit(A) 31-__builtin_clz(A)
#define endl "\n"
typedef long long ll;
typedef pair<int, int> ii; 
const int maxn = 70;
const int inf = 1e9;
int boar[maxn][maxn];
int n,m,k;

vector<int>gen(vector<int> x){
	int dp[maxn][maxn];
	for(int i=0;i<maxn;i++)
		for(int j=0;j<maxn;j++)
			dp[i][j] = -inf;
	dp[0][0] = 0; //take-resto

	for(int pos=0;pos<m;pos++){
		for(int tk=min(pos+1,m/2);tk>0;tk--){
			for(int d=0;d<k;d++){
				dp[tk][(d+x[pos])%k] = max(dp[tk][(d+x[pos])%k] , dp[tk-1][d]+x[pos]);
			}
		}
	}
	vector<int>ret(k,-inf);
	for(int d=0;d<k;d++)
		for(int tk=0;tk<=m/2;tk++)
			ret[d] = max(ret[d] , dp[tk][d]) ;
	return ret;
}
vector<int>merge(vector<int> x,vector<int>y){
	vector<int>ret(k,-inf);
	for(int i=0;i<k;i++){
		for(int j=0;j<k;j++){
			ret[(i+j)%k] = max(ret[(i+j)%k] , x[i]+y[j]);
		}
	}
	return ret;
}
int main(){
	ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
	cin>>n>>m>>k;
	vector<vector<int>> v(n,vector<int>(m));
	for(int i=0;i<n;i++)
		for(int j=0;j<m;j++)
			cin>>v[i][j];

	vector<int>lst(k,-inf);
	lst[0] = 0 ;
	for(int i=0;i<n;i++){
		lst = merge(lst,gen(v[i]));
	}
	cout<<lst[0]<<endl;
    return 0;
}


Comments

Submit
0 Comments
More Questions

1630C - Paint the Middle
1630D - Flipping Range
1328A - Divisibility Problem
339A - Helpful Maths
4A - Watermelon
476A - Dreamoon and Stairs
1409A - Yet Another Two Integers Problem
977A - Wrong Subtraction
263A - Beautiful Matrix
180C - Letter
151A - Soft Drinking
1352A - Sum of Round Numbers
281A - Word Capitalization
1646A - Square Counting
266A - Stones on the Table
61A - Ultra-Fast Mathematician
148A - Insomnia cure
1650A - Deletions of Two Adjacent Letters
1512A - Spy Detected
282A - Bit++
69A - Young Physicist
1651A - Playoff
734A - Anton and Danik
1300B - Assigning to Classes
1647A - Madoka and Math Dad
710A - King Moves
1131A - Sea Battle
118A - String Task
236A - Boy or Girl
271A - Beautiful Year